﻿using System.Text;
using System.IO;
using System.Drawing;

namespace DotNet.Common
{
    public class HtmlGenerator
    {
        string _title;
        HtmlBody _body;
        Encoding _textEncoding = Encoding.UTF8;

        public string Title
        {
            set { _title = value; }
        }

        public Encoding TextEncoding
        {
            set { _textEncoding = value; }
        }

        public HtmlBody Body
        {
            get { return _body; }
        }

        public HtmlGenerator()
        {
            _body = new HtmlBody();
            Clear();
        }

        public void Clear()
        {
            _title = string.Empty;
            _body.Clear();
        }

        public bool Generate(string filePath)
        {
            string fileContent = string.Format("<html>\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset={1}\" />\r\n<title>{0}</title>\r\n</head>\r\n<body>\r\n{2}\r\n</body>\r\n</html>", _title, _textEncoding.WebName, _body.Text);

            try
            {
                File.WriteAllText(filePath, fileContent, _textEncoding);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }

    public class HtmlBody
    {
        StringBuilder _text;

        public string Text
        {
            get { return _text.ToString(); }
        }

        public HtmlBody()
        {
            _text = new StringBuilder();
            Clear();
        }

        public void Clear()
        {
            _text.Clear();
        }

        public void Add(string text)
        {
            _text.Append(text);
        }

        public void AddLine()
        {
            _text.Append("<br>\r\n");
        }

        public void AddLine(string text)
        {
            _text.Append(text + "<br>\r\n");
        }

        public void AddPragraph(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                _text.Append("<br>\r\n");
            }
            else
            {
                this.Add(FormatTag(text, "p"));
            }
        }

        public void Add(string text, FontStyle style)
        {
            this.Add(FormatFontStyle(text, style));
        }

        public void AddLine(string text, FontStyle style)
        {
            this.AddLine(FormatFontStyle(text,style));
        }

        public void Add(string text, int size)
        {
            this.Add(FormatTag(text,"font",string.Format("size={0}",size)));
        }

        public void AddLine(string text, int size)
        {
            this.AddLine(FormatTag(text, "font", string.Format("size={0}", size)));
        }

        public void AddTable(string Name, string[] heads,string[][] elements)
        {
            if (elements == null || elements.Length == 0)
                return;

            this.AddLine();
            this.AddLine(Name);

            StringBuilder otableBody = new StringBuilder();
            otableBody.Append("<table border>\r\n");
            if (heads != null)
            {
                otableBody.Append("<tr>");
                foreach (string col in heads)
                {
                    otableBody.AppendFormat("<th>{0}</th>", col);
                }
                otableBody.AppendLine();
            }
            foreach (string[] row in elements)
            {
                otableBody.Append("<tr>");
                foreach (string element in row)
                {
                    otableBody.AppendFormat("<td>{0}</td>", element);
                }
                otableBody.AppendLine();
            }
            otableBody.Append("</table>\r\n");
            this.AddLine(otableBody.ToString());
        }

        private string FormatFontStyle(string text, FontStyle style)
        {
            if (style != FontStyle.Regular)
            {
                if ((style & FontStyle.Bold) != FontStyle.Regular)
                {
                    text = FormatTag(text, "b");
                }
                if ((style & FontStyle.Italic) != FontStyle.Regular)
                {
                    text = FormatTag(text, "i");
                }
                if ((style & FontStyle.Strikeout) != FontStyle.Regular)
                {
                    text = FormatTag(text, "s");
                }
                if ((style & FontStyle.Underline) != FontStyle.Regular)
                {
                    text = FormatTag(text, "u");
                }
            }

            return text;
        }

        private string FormatTag(string text, string tagName)
        {
            return string.Format("<{0}>{1}</{0}>\r\n", tagName, text);
        }

        private string FormatTag(string text, string tagName, string tagExpression)
        {
            return string.Format("<{0} {2}>{1}</{0}>\r\n", tagName, text,tagExpression);
        }
    }
}
